<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>修改英雄</title>
  <link href="./lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
  <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  <style>
    .wrap {
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: url('images/bg03.jpg') center bottom no-repeat;
      overflow: auto;
    }

    .navbar-brand {
      padding: 10px 15px;
    }

    .page-title {
      font-size: 16px;
      font-weight: bold;
    }

    .file-input {
      outline: none;
      display: inline-block;
      margin-top: 5px;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-horizontal {
      margin-top: 10px;
    }

    .logout {
      font-weight: 900;
      font-size: 20px;
      color: #ff0000;
      text-decoration: none;
    }

    .logout:hover {
      text-decoration: none;
      color: yellowgreen;
    }

    .preview {
      width: 100px;
      height: 100px;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <nav class="navbar  navbar-inverse navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mymenu">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="./index.html"><img src="images/logo.png"></a>
        </div>
      </div>
    </nav>


    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <div class="panel panel-default">
            <div class="panel-heading clearfix">
              <div class="row">
                <div class="col-md-6 page-title">英雄信息修改</div>
                <div class="col-md-6 text-right">当前位置：首页 &gt; 英雄信息修改</div>
              </div>
            </div>
            <div class="panel-body">
              <!-- 表单 -->
              <form id="formEdit" action="#" method="" class="form-horizontal">
                <div class="form-group">
                  <label for="heroName" class="col-sm-2 control-label">姓名</label>
                  <div class="col-sm-10">
                    <input required type="text" class="form-control" id="heroName" name="name" placeholder="请输入姓名"
                      value="盖伦" />
                  </div>
                </div>
                <div class="form-group">
                  <label for="skillName" class="col-sm-2 control-label">技能</label>
                  <div class="col-sm-10">
                    <input required type="text" class="form-control" id="skillName" name="skill" placeholder="请输入技能"
                      value="躲草丛" />
                  </div>
                </div>
                <div class="form-group">
                  <label for="heroIcon" class="col-sm-2 control-label">头像</label>
                  <div class="col-sm-10">
                    <!-- 图片预览 -->
                    <img id="imgPrev" src="./imgs/盖伦.png" class='preview' alt="">
                    <input required type="file" class="file-input" id="heroIcon" name="icon" />

                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-primary btn-save">
                      保存
                    </button>
                    <button type="button" class="btn btn-cancel">取消</button>
                  </div>
                </div>
              </form>
              <!-- 表单 end -->
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<script src="./lib/bootstrap/js/jquery-1.12.4.js"></script>
<script src="./lib/bootstrap/js/bootstrap.min.js"></script>


<script>
  // 入口函数
  $(function () {
    searchInfo();
    editInfo();
  })
  // 业务目标1： 查询英雄信息
  function searchInfo() {
    //1.修改预览图片
    $("#heroIcon").change(function () {
      //a.获取 文件选择框的 文件内容
      let fileInfo = this.files[0];
      //b.使用js 创建 选中文件的 虚拟路径
      let virPath = URL.createObjectURL(fileInfo);
      //c. 设置虚拟路径 给  预览图片
      $('#imgPrev').attr('src', virPath);
    });
    //2. 获取 url 中的 id 值
    let hId = window.location.search.split('=')[1];
    //3. 异步请求 英雄数据 ，并且显示
    $.ajax({
      url: '/search',
      method: 'get',
      data: 'id=' + hId,
      contentType: 'false',
      processData: 'false',
      success: function (backData) {
        // console.log(backData);
        $('#heroName').val(backData.data.name);
        $('#skillName').val(backData.data.skill);
        $('#heroIcon').attr('src', '/uploads' + backData.data.icon);
      },
    });
    //4.在函数内部声明一个函数(开辟局部作用域)
    return () => {
      //a.在闭包函数中返回你想要访问的变量
      return hId;
    };
  }
  // 业务目标2： 修改英雄信息
  function editInfo() {
    // 闭包调用, 获取 hId 
    hId = searchInfo()();

    //4. 保存时 提交 修改后的 英雄数据 到 服务器 /edit 接口
    $('.btn-primary').click(function () {
      //a.获取 修改后的数据
      let fd = new FormData($('#formEdit')[0]);

      //b.向 表单数据对象中 追加 id 值
      fd.append('id', hId);

      //c.将数据 异步提交到 /edit 接口路由
      $.ajax({
        url: '/edit',
        method: 'post',
        data: fd,
        contentType: false,
        processData: false,
        success: function (backData) {
          console.log(backData);
          alert(backData.msg);
          if (backData.code == 203) {
            window.location.href = '/index.html';
          }
        },
      });
    });

  }

</script>